﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class InvoiceProductDAL
    {
        private ManageStoreDataContext ctx;
        public InvoiceProductDAL()
        {
            ctx = new ManageStoreDataContext();
        }
        public Invoices_Product GetInvoiceProductByProductID(int productID)
        {
            Invoices_Product curr = ctx.Invoices_Products.Single<Invoices_Product>(pro => pro.ProductID == productID);
            return curr;
        }
        public IEnumerable<Invoices_Product> GetAllInvoiceProduct()
        {
            IEnumerable<Invoices_Product> source = from pro in ctx.Invoices_Products
                                           orderby pro.InvoiceID
                                                   select pro ;
            return source;
        }
        public bool DeleteInvoiceProductByProductID(int productID)
        {
            Invoices_Product curr = ctx.Invoices_Products.Single<Invoices_Product>(pro => pro.ProductID == productID);
            if (curr == null)
                return false;
            ctx.Invoices_Products.DeleteOnSubmit(curr);
            try
            {
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }
        
        public bool CheckDuplicateInvoiceProductByInvoiceID(int productID)
        {
            var invoices = from i in ctx.Invoices_Products
                           select i;
            if (invoices == null)
                return false;
            else
            {
                Invoices_Product curr = ctx.Invoices_Products.SingleOrDefault<Invoices_Product>(pro => pro.ProductID.Equals(productID));
                if (curr == null)
                    return false;
                return true;
            }
        }
        public bool UpdateInvoiceProduct(Invoices_Product inproduct)
        {
            Invoices_Product curr = ctx.Invoices_Products.Single<Invoices_Product>(pro => pro.ProductID == inproduct.ProductID);

            if (curr == null)
                return false;
            curr.InvoiceID = inproduct.InvoiceID;
            curr.Price = inproduct.Price;
            curr.Quantity =inproduct.Quantity;
            try
            {
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }
        public bool InsertInvoiceProduct(Invoices_Product inproduct)
        {
            try
            {
                ctx.Invoices_Products.InsertOnSubmit(inproduct);
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }  
    }
}
